<?php


namespace App\Repositories\Admin\User;


use App\Models\User;
use App\Repositories\BaseRepository;
use Illuminate\Support\Facades\Config;

class UserRepository extends BaseRepository
{

    public function model()
    {
        // TODO: Implement model() method.
        return new User();
    }
    public function pagination($where=[],$param=[])
    {
        $size = (isset($param['size']) && $param['size']) > 0 ? (int)$param['size'] : (int)Config::get('admin.per_page');
        $field = $param['field']??['*'];
        $betweens = $param['between']??[];
        $builder = $this->model();
        $builder = $builder->when($where, function ($query) use ($where) {
            return $query->where($where);
        });
        foreach ($betweens as $between){
            $builder = $builder->whereBetween($between['key'],[$between['start'],$between['end']]);
        }
        $page = $builder->select($field)->paginate($size);
        return $page;
    }

    public function create($data)
    {
        $insertData = [];
        $insertData['nickname'] = $data['nickname'];
        $insertData['desc'] = $data['desc']??'';
        $insertData['status'] = $data['status']??true;
        $insertData['mobile'] = $data['mobile'];
        $insertData['password'] = $data['password'];
        return (new static())->model()->create($insertData);
    }

    public function allotRoles(array $roles, User $user)
    {
        return $user->syncRoles($roles);
    }

    public function allotPermissions(array $permissions, User $user)
    {
        return $user->syncPermissions($permissions);
    }
}
